#!/bin/bash

cn=`head -n 4 $XRDBASEDIR/admin/server/cmsd.pid | tail -n 1 | awk -F '=' '{ print $2 }'`

export APMON_CONFIG=${MONALISA_HOST}
#unset APMON_CONFIG
#o=`$1/bin/xrdcp -np -S 15 -DIConnectTimeout 30 -DIConnectTimeoutWan 30 -DIFirstConnectMaxCnt 3 -DIMaxRedirectcount 10 -OStried=+$cn $2 $3 2>&1`
#r=$?

# tmp for Martin Vala to see return Values (will be removed)
#echo "$o $r" > /tmp/retValues

# if [ "$r" != "0" ]; then

#   # getting file name only (without redirector name)
    MYFILE=`echo $2 | awk -F '//' '{print $3}'`

    XRDDM_OPTIONS="-a -S -R -s aliengr,alien,aliencp"
    source $1/etc/xrddm_env.sh $1
    echo "source $1/etc/xrddm_env.sh $1"> /tmp/xrddm-last-cmd.log
#     source /tmp/gclient_env_$UID
    XRDDMCMD_TMP="$XRDDMSYS/bin/xrddm $XRDDM_DEBUG $XRDDM_OPTIONS -c $XRDDMSYS/etc/xrddm.cfg -L $AAF_DIR/logs/xrddm.log -l $AAF_DIR/logs/xrddm -t $cn alien:///$MYFILE $3"
    echo "$XRDDMCMD_TMP" >> /tmp/xrddm-last-cmd.log
    o=`$XRDDMCMD_TMP`
    r=$?
    o="$XRDDMCMD_TMP"

echo $r
echo $o
